﻿using System.Collections.Generic;

namespace g_gongjianta.battle.fsm
{
    public abstract class AbstractMultiFsmState : AbstractFsmState, IFSM
    {
        private readonly Dictionary<int, AbstractFsmState> dict = new();

        public AbstractFsmState Curr { get; protected set; }

        public void Add<T>() where T : AbstractFsmState
        {
            var state = Entity.Create<T, IFSM, IFSM>(Root, this);
            dict[state.Kind] = state;
        }

        public T Get<T>(int kind)
          where T : AbstractFsmState
        {
            if (dict.ContainsKey(kind) == false)
            {
                return default;
            }
            return dict[kind] as T;
        }

        public override void Enter()
        {

        }

        public override void Exit()
        {
            Curr?.Exit();
            Curr = null;
        }

        public override void Update()
        {
            Curr?.Update();
        }

        public void Switch(int state, int skill_id = 0)
        {
            if (Curr != null)
            {
                Curr.Exit();
                Curr = null;
            }
            if (dict.ContainsKey(state))
            {
                Curr = dict[state];

                Curr.SkillId = skill_id;

                Curr.Enter();
            }
        }


    }
}
